/**
 * @file    drv_lpm.c
 * @brief   低功耗管理
 * @author  Liu Wei
 * @version 1.0.1
 * @date    2023-06-17
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-06-17 <td>1.0.1     <td>Liu wei  <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "drv_lpm.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/
static void drv_lmp_wk_cfg(uint32_t u32PDMode, uint32_t u32EdgeType);
/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/
uint8_t sleep_wk_src = 0;
/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
/**
 * @brief  进入休眠模式
 * @param  lmp_mode : 休眠模式
 * 	       LMP_MODE_IDLE      
 * 		   LMP_MODE_LOW_POW   
 * 		   LMP_MODE_SHUT_DOWN 
 * @param  wk_pin
 * 		   LMP_WK_CHG_PIN 
 * 		   LMP_WK_COM_PIN
 * 		   LMP_WK_KEY_PIN
 *         LMP_WK_CAN_PIN
 * @param  wk_time
 */
void drv_lpm_into_mode(lmp_mode_type lmp_mode ,uint32_t wk_pin , uint32_t wk_time)
{
	uint32_t wk_pin_src = 0;
	
	switch(lmp_mode)
	{
		case LMP_MODE_IDLE:
			break;
		case LMP_MODE_LOW_POW:
			break;
		case LMP_MODE_SHUT_DOWN:
			if((LMP_WK_CHG_PIN & wk_pin) == LMP_WK_CHG_PIN)
			{
				wk_pin_src |= CLK_DPDWKPIN2_FALLING;
			}

			if((LMP_WK_COM_PIN & wk_pin) == LMP_WK_COM_PIN)
			{
				wk_pin_src |= CLK_DPDWKPIN4_RISING;
			}

			if((LMP_WK_KEY_PIN & wk_pin) == LMP_WK_KEY_PIN)
			{
				wk_pin_src |= CLK_DPDWKPIN_FALLING;
			}

			if((LMP_SW_KEY_PIN & wk_pin) == LMP_SW_KEY_PIN)
			{
				wk_pin_src |= CLK_DPDWKPIN1_BOTHEDGE;
			}

			drv_lmp_wk_cfg(CLK_PMUCTL_PDMSEL_DPD , wk_pin_src);
			break;
		default:
			break;
	}
}

void drv_lmp_into_idle_sleep(void)
{
	SYS_UnlockReg();
			
	CLK_SetPowerDownMode(CLK_PMUCTL_PDMSEL_LLPD);
			
	CLK_PowerDown();
	
	SYS_LockReg();
}

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */

/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
void drv_lmp_wk_cfg(uint32_t u32PDMode, uint32_t u32EdgeType)
{
	SYS_UnlockReg();
	/* Enable RTC peripheral clock */
    CLK->APBCLK0 |= CLK_APBCLK0_RTCCKEN_Msk;
    /* RTC clock source select LXT */
    CLK->CLKSEL3 &= ~(CLK_CLKSEL3_RTCSEL_Msk);
    /* Open RTC and start counting */
    RTC->INIT = RTC_INIT_KEY;
    if(RTC->INIT != RTC_INIT_ACTIVE_Msk) {
        RTC->INIT = RTC_INIT_KEY;
        while(RTC->INIT != RTC_INIT_ACTIVE_Msk);
    }	
	/* Clear tick status */
    RTC_WaitAccessEnable();
    RTC_CLEAR_TICK_INT_FLAG();
	
	/* Select Power-down mode */
    CLK_SetPowerDownMode(u32PDMode);
	
    GPIO_SetMode(PB, BIT0, GPIO_MODE_INPUT);
	PB->PUSEL = 0x0000001; // 内部上拉
	PH->PUSEL = 0x0010000; // 内部上拉
	GPIO_SetMode(PB, BIT2, GPIO_MODE_INPUT);
	GPIO_SetMode(PB, BIT12, GPIO_MODE_INPUT);
	GPIO_SetMode(PF, BIT6, GPIO_MODE_INPUT);
	/* Configure GPIO as Input mode */

    CLK_EnableDPDWKPin(u32EdgeType);
	CLK->PMUCTL |= CLK_PMUCTL_DPDHOLDEN_Msk;//Jeff
	RTC->GPIOCTL0 = 0x09080D0D;//ywb GPIO controlled by VBAT power domain
	RTC->GPIOCTL1 = 0x0D0D0D0D;

    /* Enter to Power-down mode */
    CLK_PowerDown();

    /* Wait for Power-down mode wake-up reset happen */
    while(1);
    /* Wait for Power-down mode wake-up reset happen */
//    while(1);
}
 
/******************************(C)Copyright end *******************************/

